﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class PaymentsSortService
    {
        /// <summary>
        /// 查询收支分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PaymentsSort GetPaymentsSort(long id)
        {
            PaymentsSort paymentsSort = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    paymentsSort = new PaymentsSort();
                    paymentsSort.Id = id;
                    paymentsSort = new PaymentsSortDAO().GetPaymentsSort(conn, paymentsSort);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return paymentsSort;
        }

        /// <summary>
        /// 查询收支分类列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="enterpriseNo"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public DataTable GetPaymentsSortTable(int pageindex, int pagesize, String enterpriseNo, int type)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    PaymentsSort paymentsSort = new PaymentsSort();
                    paymentsSort.Pageindex = pageindex;
                    paymentsSort.Pagesize = pagesize;
                    paymentsSort.EnterpriseNo = enterpriseNo;
                    paymentsSort.Type = type;
                    dt = new PaymentsSortDAO().GetPaymentsSortTable(conn, paymentsSort);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询收支分类记录数
        /// </summary>
        /// <param name="type"></param>
        /// <param name="enterpriseNo"></param>
        /// <returns></returns>
        public long GetPaymentsSortRecord(String enterpriseNo, int type)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    PaymentsSort paymentsSort = new PaymentsSort();
                    paymentsSort.EnterpriseNo = enterpriseNo;
                    paymentsSort.Type = type;
                    record = new PaymentsSortDAO().GetPaymentsSortRecord(conn, paymentsSort);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入收支分类
        /// </summary>
        /// <param name="paymentsSort"></param>
        /// <returns></returns>
        public long InsertPaymentsSort(PaymentsSort paymentsSort)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = new PaymentsSortDAO().InsertPaymentsSort(conn, paymentsSort);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新收支分类
        /// </summary>
        /// <param name="paymentsSort"></param>
        /// <returns></returns>
        public int UpdatePaymentsSort(PaymentsSort paymentsSort)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new PaymentsSortDAO().UpdatePaymentsSort(conn, paymentsSort);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除收支分类
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeletePaymentsSort(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        PaymentsSortDAO paymentsSortDAO = new PaymentsSortDAO();
                        PaymentsSort paymentsSort = new PaymentsSort();
                        foreach (String lopid in arr)
                        {
                            paymentsSort.Id = Int64.Parse(lopid);
                            count += paymentsSortDAO.DeletePaymentsSort(conn, paymentsSort);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
